/**
 * Project Name:九色鹿
 * File Name:CustomerWebMvcConfigurerAdapter.java
 * Package Name:com.fxp.project.jiuselu.admin.config
 * Date:2020年03月21日 21:27
 * Copyright (c) 2020, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.web.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:CustomerWebMvcConfigurerAdapter<br/>
 * Function: 自定义控制器<br/>
 * Date:2020年03月21日 21:27<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0.0
 * @see
 * @since JDK1.8
 */
@Configuration
public class CustomerWebMvcConfigurerAdapter extends WebMvcConfigurationSupport {
    /**
     * configureMessageConverters: 利用fastjson替换掉jackson<br/>
     *
     * @param converters
     * @author Jason.Wang
     * @createTime 2020/3/21 21:29
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //1.构建了一个HttpMessageConverter  FastJson   消息转换器
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2.定义一个配置，设置编码方式，和格式化的形式
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //3.设置成了PrettyFormat格式
        //QuoteFieldNames———-输出key时是否使用双引号,默认为true
        //WriteMapNullValue——–是否输出值为null的字段,默认为false
        //WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
        //WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
        //WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
        //WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse,SerializerFeature.WriteDateUseDateFormat);
//        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
//        serializeConfig.put(LocalDateTime.class, LocalDateTimeToTimestampSerializer.instance);
//        fastJsonConfig.setSerializeConfig(serializeConfig);
        //4.处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastJsonConfig.setCharset(StandardCharsets.UTF_8);
        fastMediaTypes.add(MediaType.APPLICATION_JSON);

        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        //5.将fastJsonConfig加到消息转换器中
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }
}
